package src.main.java.org.thread.demo2;

import java.util.List;
import java.util.Random;

public class SendThread extends Thread{
    private List<String> gift;
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public SendThread(List<String> gift, String name){
        super(name);
        this.gift = gift;
    }
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        //小明、小红发礼物
        //实现线程安全
        //注意1：锁必须唯一
        Random random = new Random();
        while (true){
            synchronized (gift){
                if(gift.size()<10){
                    break;
                }
                String rs = gift.remove(random.nextInt(gift.size()));
                System.out.println(name+"发出了："+rs);
                count++ ; //加各自的count
            }
        }

    }
}
